// pages/talk/shaixuan/shaixuan.js
import { request } from '../../../utils/http.js';
const options = [
  {
    text: '浙江省',
    value: '330000',
    children: [{ text: '杭州市', value: '330100' }],
  },
  {
    text: '江苏省',
    value: '320000',
    children: [{ text: '南京市', value: '320100' }],
  },
  {
    text: '河南省',
    value: '410000',
    children: [
      { text: '郑州市', value: '410100' },
      { text: '开封市', value: '410200' },
      { text: '洛阳市', value: '410300' },
      { text: '平顶山市', value: '410400' },
      { text: '安阳市', value: '410500' },
      { text: '鹤壁市', value: '410600' },
      { text: '新乡市', value: '410700' },
      { text: '焦作市', value: '410800' },
      { text: '濮阳市', value: '410900' },
      { text: '许昌市', value: '411000' },
      { text: '漯河市', value: '411100' },
      { text: '三门峡市', value: '411200' },
      { text: '南阳市', value: '411300' },
      { text: '商丘市', value: '411400' },
      { text: '信阳市', value: '411500' },
      { text: '周口市', value: '411600' },
      { text: '驻马店市', value: '411700' }
    ]
  }
];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    num:1,
    fieldValue: '',
    cascaderValue: '',
    diyu:'',
    options,
    "show1": false,
    "level": '选择等级',
    "selected_level": 1,
    "levels": [{"id":1,"level":"国家一级心理咨询师"},
        {"id":2,"level":"国家二级心理咨询师"},
        {"id":3,"level":"国家三级心理咨询师"}],
    shanchanglist:[],
    result: [],
    num1:0,
    zixun:0
  },
 
  onClick() {
    this.setData({
      show: true,
    });
  },

  onClose() {
    this.setData({
      show: false,
    });
  },

  onFinish(e) {
    const { selectedOptions, value } = e.detail;
    const fieldValue = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    const diyu = fieldValue.substring(fieldValue.indexOf('/') + 1);
        console.log(diyu);
    this.setData({
      fieldValue,
      cascaderValue: value,
      show: false,
      diyu
    })
  },
  showPopup() {
    this.setData({ show: true });
  },
  showPopup1() {
    this.setData({ show1: true });
  },


  onClose1() {
    this.setData({ show1: false });
  },
  onDian(){
    this.setData({ num1: 0 });
  },
  onDian1(){
    this.setData({ num1: 1 });
  },
  onDian2(){
    this.setData({ num1: 2 });
  },
  onZi(){
    this.setData({ zixun: 0 });
  },
  onZi1(){
    this.setData({ zixun: 1 });
  },
  onZi2(){
    this.setData({ zixun: 2 });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    request({
      url: '/shaixuan/listAll',
      method: 'GET',
      data: { key: 'value' }
    }).then((data) => {
      console.log(data);
      console.log(data.data);
      this.setData({
        shanchanglist: data.data,
      });
    }).catch((err) => {
      console.error(err);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onb1(){
    this.setData({num:1})
  },
  onb2(){
    this.setData({num:2})
  },
  onJieguo(e){
    console.log(e);
    getApp().globalData.diyu = this.data.diyu
    getApp().globalData.result = this.data.result
    getApp().globalData.level = this.data.level
    getApp().globalData.zixun = this.data.zixun
    wx.navigateTo({
      url: '/pages/talk/shaixuan/jieguo/jieguo'
    });
  },
  onchongzhi(){
    this.setData({
      diyu:'',
      level: '选择等级',
      result: [],
      zixun:0,
      fieldValue: '',
      num1:0
    })
  },


  select_level(msg) {
    console.log(msg);
    this.setData({
      "show1": true
    });
  },
  onClose1(msg) {
    console.log("关闭");
    this.setData({
      "show1": false
    });
  },
  click_level(msg) {
    this.setData({
      "selected_level": msg.currentTarget.id,
      "level": msg.currentTarget.dataset.text
    });
    this.onClose1();
  },
  handleButtonClick(event) {
    const index = event.currentTarget.dataset.index;
    if(this.data.shanchanglist[index].stats==0){
      if(this.data.result.length<3){
        this.data.shanchanglist[index].stats=1;
        this.data.result.push(this.data.shanchanglist[index].id);
      }
    }else{
      if(this.data.result.length==0){
        for(let i=0;i<this.data.shanchanglist.length;i++){
          this.data.shanchanglist[i].stats=0;
        }
        this.data.shanchanglist[index].stats=1;
        this.data.result.push(this.data.shanchanglist[index].id);
      }else{
        this.data.shanchanglist[index].stats=0;
        for(let i=0;i<this.data.result.length;i++){
          if(this.data.result[i]==this.data.shanchanglist[index].id){
            this.data.result.splice(i,1);
          }
        }
      }
    }


    this.setData({
      shanchanglist:this.data.shanchanglist,
      result:this.data.result
    });
  },
  handleButtonClick1(){
    if(this.data.result.length!=0){
      this.data.result =  [];
      for(let i=0;i<this.data.shanchanglist.length;i++){
        this.data.shanchanglist[i].stats=1;
      }
    }
    this.setData({
      shanchanglist:this.data.shanchanglist,
      result:this.data.result
    });
  }
})